import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm
} from 'comsMobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {
	Button,
} from 'antd';
import {
   isArray
} from 'lodash';
const getLabel = WeaLocaleProvider.getLabel;

export default class SearchStore {
	constructor(root) {
		this.root = root;
	}
	
	advanceHeight = 130;
	hasMask = false;
	keyParam = 'key';

	otherProps = {};

	@observable datas = [];
	@observable selectedKey = '0';
	@observable form = new WeaForm();
	@observable conditions = [];
	@observable showSearchAd = false;
	@observable searchType = ['base', 'advanced'];
	@observable mark = '';

	@action init = (config) => {
		Object.keys(config).forEach(key => {
			this[key] = config[key];
		})
	}

	@computed get searchsBaseValue() {
		return this.form.getFormParams()[this.linkageFieldName]
	}

	@computed get searchsAd() {
		return (
			<div  onKeyDown={this.onKeyDown}>{this.form.render()}</div>
		);
	}
	

	get buttons() {
		const mark = (this.mark !== undefined) ? this.mark : '';
		return this.root[`${mark}SearchButtons`];
	}

	get buttonsAdNames() {
		return [getLabel(82529, '搜索'), getLabel(27088, '重置'), getLabel(32694, '取消')];
	}

	get buttonsAd() {
		return this.buttonsAdNames.map((name, index) => {
			const type = (index === 0) && "primary";

			let callback;
			if (index === 0) {
				callback = () => this.handleSearch();
			} else if (index === 1) {
				callback = () => {
					if (this.isFormIncludeSelect) {
						this.form.resetConditionValue();
					} else {
						this.form.reset();
					}
				}
			} else {
				callback = () => this.setShowSearchAd(false)
			}

			return <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9er7ed@${index}`} type={type} onClick={callback}>{name}</Button>
		})
	}

	get isFormIncludeSelect() {
		return this.conditions.some(condition => {
			return condition.items.some(item => {
				const {
					conditionType
				} = item;

				return conditionType === 'SELECT'
			})
		})
	}

	@action fetchSearchForm = (params = {}) => {
		this.form = new WeaForm();

		return new Promise((resolve, reject) => {
			this.api(params).then(datas => {
				const conditions = this.getCondition(datas);

				this.conditions = conditions;
				this.form.setCondition(conditions);

				resolve();
			});
		})
	}

	@action fetchTabs = (params = {}) => {
		return new Promise((resolve, reject) => {
			this.tabApi(params).then(datas => {
				const {
					status,
				} = datas;

				if (status === '1') {
					this.setDatas(this.getTabs(datas));

					resolve();
				} else {
					message.error(datas.message);
				}
			})
		})
	}

	@action setDatas = (datas) => {
		this.datas = datas;
	}

	@action setSelectedKey = (selectedKey) => {
		this.selectedKey = selectedKey;
	}

	@action setShowSearchAd = (showSearchAd) => {
		this.showSearchAd = showSearchAd;
	}

	handleTabChange = (selectedKey) => {
		this.setSelectedKey(selectedKey);

		this._handleTabChange && this._handleTabChange();
	}

	handleSearch = (v) => {
		this.setShowSearchAd(false);
		this._handleSearch && this._handleSearch(v);
	}

	handleSearchChange = (val) => {
		if (this.linkageFieldName) {
			this.form.updateFields({
				[this.linkageFieldName]: val
			});
		}
	}

	getCondition = (datas) => {
		let condition = [];
		Object.keys(datas).forEach(key => {
			if (key.includes('condition')) {
				condition = datas[key];
			}
		})
		return condition;
	}

	getTabs = (datas) => {
		let tabs;

		Object.values(datas).forEach(value => {
			if (isArray(value)) {
				tabs = value;
			}
		});

		return tabs;
	}

	onKeyDown = (e) =>{
		if (e.keyCode == 13&& e.target.tagName === "INPUT") {
		 this.handleSearch()
		}
	  }
 

}